from flask import Flask,render_template,request,escape
from API import tianqi,place
from log_request import log_request
import pandas as pd
app=Flask(__name__)

@app.route('/')
def hello() -> "html":
	return render_template("home.html",the_title="欢迎进入我的网站~")

@app.route('/home')
def hello_home() -> "html":
	return render_template("home.html",the_title="欢迎回到我的网站~")

@app.route('/login',methods=["GET"])
def hello_login():
	return render_template("login.html",the_title="欢迎进入登录界面~")

@app.route('/chenggongdenglu', methods=["POST"])
def hello_chenggongdenglu():
		return render_template("chenggongdenglu.html", the_title="恭喜您已经登录成功！")

@app.route('/register',methods=["GET"])
def hello_register():
	return render_template("register.html",the_title="欢迎进入注册界面~")

@app.route('/chenggongzhuce', methods=["POST"])
def hello_chenggongzhuce():
		return render_template("chenggongzhuce.html", the_title="恭喜您已经注册成功！")

@app.route('/neirong',methods=["GET","POST"])
def hello_neirong() -> "html":
	return render_template("neirong.html",the_title="欢迎来到网站内容页面~")

@app.route('/weather', methods=['POST'])
def hello_weather() -> 'html':
	key = '5bd1ca4e7bc885ff80882a4f8b45093b'
	city = request.form['city']
	title = '查询结果如下：'
	res = tianqi(key, city)

	result_humidity = res['humidity']
	result_winddirection = res['winddirection']
	result_windpower =res['windpower']
	result_temperature = res['temperature']
	result_weather = res['weather']
	result_reporttime =res['reporttime']

	log_request(request,res)
	return render_template("tianqi_results.html",
						   city=city,
						   the_title=title,
						   result_humidity=result_humidity,
						   result_winddirection=result_winddirection,
						   result_windpower=result_windpower,
						   result_temperature =result_temperature,
						   result_weather=result_weather,
						   result_reporttime =result_reporttime)



@app.route('/POI', methods=['POST'])
def hello_POI2() -> 'html':
	key = '5bd1ca4e7bc885ff80882a4f8b45093b'
	city = request.form['city']
	keywords=request.form['keywords']
	title = ['省份','区/县','具体地址','类型','名字']
	ji = place(key,keywords,city)
	log_request(request, ji)

	result_pname=[]
	result_adname=[]
	result_address=[]
	result_type=[]
	result_name=[]


	for i in ji:
		result_pname.append(i['pname'])
		result_adname.append(i['adname'])
		result_address.append(i['address'])
		result_type.append(i['type'])
		result_name.append(i['name'])

	return render_template("POI_results.html",
						   city=city,
						   the_title=title,
						   keywords=keywords,
						   result_pname=result_pname,
						   result_adname =result_adname,
						   result_address=result_address,
						   result_type=result_type,
						   result_name =result_name)



#日志系统
@app.route('/viewlog',methods=['POST','GET'])
def view_the_log() -> 'html':
    contents = []
    with open('vsearch1.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('Form Data','Remote_addr','User_agent','Results')
    return render_template('viewlog.html',
                           the_title='View Log',
                           row_title=titles,
                           the_data=contents)


if __name__=='__main__':
    app.run(debug=True)